package com.zxgj.datadirect.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxgj.common.annotation.Log;
import com.zxgj.common.core.controller.BaseController;
import com.zxgj.common.core.domain.AjaxResult;
import com.zxgj.common.core.page.TableDataInfo;
import com.zxgj.common.enums.BusinessType;
import com.zxgj.datadirect.entity.DataDirectOuterApply;
import com.zxgj.datadirect.service.DataDirectOuterApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 外来申请管理Controller
 * 
 * @author ruoyi
 */
@Api(tags = "外来申请管理")
@RestController
@RequestMapping("/datadirect/outerApply")
@RequiredArgsConstructor
public class DataDirectOuterApplyController extends BaseController {

    private final DataDirectOuterApplyService outerApplyService;

    /**
     * 分页查询外来申请列表
     */
    @ApiOperation("分页查询外来申请列表")
    @PreAuthorize("@ss.hasPermi('datadirect:outerApply:list')")
    @GetMapping("/page")
    public TableDataInfo getOuterApplyPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam("目标机构编码") @RequestParam(required = false) String targetOrgCode,
            @ApiParam("审批状态") @RequestParam(required = false) Integer approveStatus) {
        
        startPage();
        Page<DataDirectOuterApply> page = new Page<>(pageNum, pageSize);
        Page<DataDirectOuterApply> result = outerApplyService.getOuterApplyPage(page, targetOrgCode, approveStatus);
        return getDataTable(result.getRecords());
    }

    /**
     * 查看外来申请详情
     */
    @ApiOperation("查看外来申请详情")
    @PreAuthorize("@ss.hasPermi('datadirect:outerApply:query')")
    @GetMapping("/{applyNo}")
    public AjaxResult getOuterApplyDetail(@ApiParam("申请单号") @PathVariable String applyNo) {
        DataDirectOuterApply apply = outerApplyService.getByApplyNo(applyNo);
        return success(apply);
    }

    /**
     * 批量审批外来申请
     */
    @ApiOperation("批量审批外来申请")
    @PreAuthorize("@ss.hasPermi('datadirect:outerApply:approve')")
    @Log(title = "审批外来申请", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    public AjaxResult batchApprove(
            @ApiParam("申请单号列表") @RequestBody List<String> applyNos,
            @ApiParam("审批状态：1-通过，2-驳回") @RequestParam Integer approveStatus,
            @ApiParam("审批意见") @RequestParam String approveOpinion) {
        
        String approveUser = "admin"; // 临时使用固定值，实际应该从登录用户获取
        int successCount = outerApplyService.batchApprove(applyNos, approveStatus, approveOpinion, approveUser);
        return success("审批成功");
    }

    /**
     * 标记申请已读
     */
    @ApiOperation("标记申请已读")
    @PreAuthorize("@ss.hasPermi('datadirect:outerApply:read')")
    @PostMapping("/markRead/{applyNo}")
    public AjaxResult markAsRead(@ApiParam("申请单号") @PathVariable String applyNo) {
        boolean result = outerApplyService.markAsRead(applyNo);
        return result ? success("标记成功") : error("标记失败");
    }

    /**
     * 统计待审批数量
     */
    @ApiOperation("统计待审批数量")
    @PreAuthorize("@ss.hasPermi('datadirect:outerApply:count')")
    @GetMapping("/countPending")
    public AjaxResult countPending(@ApiParam("目标机构编码") @RequestParam String targetOrgCode) {
        int count = outerApplyService.countPendingByTargetOrg(targetOrgCode);
        return success(count);
    }

    /**
     * 获取待审批的外来申请列表
     */
    @ApiOperation("获取待审批的外来申请列表")
    @PreAuthorize("@ss.hasPermi('datadirect:outerApply:list')")
    @GetMapping("/pending")
    public AjaxResult getPendingList(@ApiParam("目标机构编码") @RequestParam String targetOrgCode) {
        List<DataDirectOuterApply> list = outerApplyService.getPendingByTargetOrg(targetOrgCode);
        return success(list);
    }

    /**
     * 同步国家平台外来申请数据
     */
    @ApiOperation("同步国家平台外来申请数据")
    @PreAuthorize("@ss.hasPermi('datadirect:outerApply:sync')")
    @Log(title = "同步外来申请数据", businessType = BusinessType.OTHER)
    @PostMapping("/sync")
    public AjaxResult syncFromNationalPlatform() {
        int syncCount = outerApplyService.syncFromNationalPlatform();
        return success("同步完成");
    }
}
